package com.coolpad.job.device.util;

import com.coolpad.Utils.AESUtils;
import com.coolpad.Utils.ExtAssert;
import com.coolpad.Utils.RSAUtils;
import com.coolpad.job.device.model.RequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * 功能描述:加密解密安全工具<br/>
 *
 * @Author:shengtao_yang
 * @Date: 2020/12/17 14:03
 * @since: 1.0.0
 */
@Slf4j
public class SecurityUtils {

    private static String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKNlZcCYvk3QD8MI6urvSlI/rWU0tuMtZ6H4hBUTp0njUx2Am5WbWt0CYnrmyiL+36ErpMIJOtF1JvHEG8wxqS6MJmj/GwD1HUdp499iPJh4Rmj6O96tFmnkyAH/bWbAIG3X41YTMyzV4LS+qrX2q0YLqLVbZi2VmwgsxjSCvGA5AgMBAAECgYByfgB4ng7rM1l4Dm2dxaLMUzZK7k8gjJZLVnom0qisC5AX4iAjCsL2jk83UzRcpT9KDeza0eDXGEH7srDPuhSTGdsxe9kFbmFBNPm6BrHAVUqaTDBLlTuoyCMhicuWjIU7dQxEtAMxcyFC+Kji5QOA8jr6lThnuiD08a7AyodTaQJBAOrxtNeQeu26L54ZPW71/yk3hczeJoYzQsiwtb4z+wI+z6k+HKO+Vbb5QGnO/3hp9oxOaxxwksCVWJm80bulmO8CQQCyCiw0anCUs34+/hHxbR01+mLVLsJ03rn1dYL4d/Sh+cov5Uoz0bTHaUm1Pr5o5uO9lrcEjbt2mDoOh0gUOwlXAkBLQSCr65t0ii62dV84K5OPoDk7ZezxD3pUzkVopz2/el8J1ttVl6Hc+fmn6qXp7AcM6hFlCqn3Bd9bXx/O+zwBAkAC/xbVXwE8D3VNfBszNAHs2aT2L45IR0Nb9TlaEzLD0x9rBPHlw17GiGgcK80qmNA2baXw5q3EBjXb7rzw0jmLAkBcI3VqPYTkR1I7zjGQNZwMS++Ytt1h5+9H/JCfPbLWCr2GZQs7i/VOk4Arh0h0ZvWYRtbqhsnjeH0qAahc3LSB";
    private static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjZWXAmL5N0A/DCOrq70pSP61lNLbjLWeh+IQVE6dJ41MdgJuVm1rdAmJ65soi/t+hK6TCCTrRdSbxxBvMMakujCZo/xsA9R1HaePfYjyYeEZo+jverRZp5MgB/21mwCBt1+NWEzMs1eC0vqq19qtGC6i1W2YtlZsILMY0grxgOQIDAQAB,";

    public static void main(String[] args) throws Exception{
        String key = AESUtils.getKey();
        String ekey = RSAUtils.encrypt(key, publicKey);

        String s = "{ \n" +
                "        \"imei1\": \"867305032765328\", \n" +
                "        \"m\": \"COOLPAD X1\", \n" +
                "        \"loc\": [ \n" +
                "                { \n" +
                "                        \"ts\": 1502848753632,\n" +
                "                        \"ip\": \"157.50.9.29\", \n" +
                "                        \"gps\": \"43.53434#34.2324\", \n" +
                "                        \"conc_wf\": \"73:3a:0e:a4:d5:90,MIOffice,-16\", \n" +
                "                        \"near_wf\":\"70:3a:0e:a4:d5:90,MIOffice,-36; 70:3a:0e:a4:d5:90,MIOffice,-36\", \n" +
                "                        \"cell\":\"460,00,4530,22652166; 460,00,4530,22652166\"\n" +
                "                } \n" +
                "        ] \n" +
                "}";
        String encrypt = AESUtils.encrypt(s, key);
        System.out.println(decrypt(encrypt,ekey));
        System.out.println("加密:" + encrypt);
        System.out.println("ekey:" + ekey);
        String s1 = "dPqjTPw+q+lryB0TRuCZsSWL34hYfvXWDDwdOi/opRSs/TuNRH/lap4Z1DrQzO3TgCqcaWdhd7SxDFluNWDI6ViRUjsn7Xa7qfG4JZkI76RvPK/28CTOht2mi/+H9knDmhH6Bt/uG68ODxf/RqqPPlH/hPmSrUxfqib9Tb+C+2laWrfZeVWKEz0Wjkm5yuGLoq8ZtrjFK8YPUNrQCZeuPcqjxE2nCUi2YF2GgX3g3OqK+dsIY6pHBTpzolR7UGTllKByghQNQEiA6NGV6r1HFxx9e/lksJQtzeu61U6JiAa5XWBceMNmg+9ICOAIpwWqHtGcf1J61LkKiDMsF9k3xFH/hPmSrUxfqib9Tb+C+2mUTXKXQdRCmG5G0lYypook6wLY2w2eR8cBaUeY8iJwQLnczK90CgbytpS77QesaB9R/4T5kq1MX6om/U2/gvtpS9PLf6hunJp504WJAzXz+Sai8kZIrdLdf+UzeNd8w0mT3YTXeaGTJBZSwgPsoD/6kwSrfygUN0qnfIQTzv6yy+6E4BEN8OR47Pq/jZN7Lrb0hqqTbZOvCxb+tj24v82pVg7rpg7YVEQ+ebGjtGigbd6OuQg5kI+vkZSjApLZR437jMUdhe3Q9W89MIDDStqPYrLiG8Zm+KJw17bnGBcI3h9IWwVq7OhoRNBILwb4ca5CN3y4xQQyXYj2c4yNbGk3";
        String s2 = "aCs+SpUgfBuODzjwMwozvOWOfQoKcjgnII7RbleRCiGmg3Kvkui3FX8fink4Zfs+mjT6bnpDfaHkH2cnp41JpITp+QqX1n0dath9oI2uA1tIJcEbn47KTw/z/kdTHt9fDDCwzWv66qVFIrBp8UrUsNYYVjxf4R70ZvlEIWHcRnU=";
        System.out.println(decrypt(s1,s2));

    }

    /**
     * decrypt
     * @param data
     * @param encrKey
     * @return
     * @throws Exception
     */
    public static String decrypt(String data, String encrKey) {

        try {
            return AESUtils.decrypt(data,new String(RSAUtils.decrypt(encrKey,privateKey)));
        } catch (Exception e) {
            log.error("decrypt exception data: {} encryptKey：{}",data, encrKey);
            throw new RuntimeException(e);
        }
    }

    /**
     * decrypt request body
     * @param requestDTO
     * @return
     */
    public static String decryptRequest2Json(RequestDTO requestDTO) {
        ExtAssert.hasText(requestDTO.getData(),"illegal param");
        return  StringUtils.hasText(requestDTO.getEncryptKey())
                ? SecurityUtils.decrypt(requestDTO.getData(), requestDTO.getEncryptKey())
                : requestDTO.getData();
    }
}
